<template>
  <div class="CharacterVerification" :style="{ 'width': width + 'px', 'height': height + 'px' }"
    ref="CharacterVerification">
    <canvas @click="refresh" :width="width" :height="height" ref="verifyCanvas" :style="{ cursor: 'pointer' }"></canvas>
  </div>
</template>

<script lang="ts">
import { defineComponent, ref, onMounted } from 'vue'
export default defineComponent({
  name: 'Identify',
  props: {
    width: {
      type: Number,
      default: 120
    },
    height: {
      type: Number,
      default: 37
    },
    type: {
      type: String,
      default: 'blend' // 图形验证码默认类型blend:数字字母混合类型、number:纯数字、letter:纯字母
    }
  },
  setup (props, { expose }) {
    const numArr = '0,1,2,3,4,5,6,7,8,9'.split(',')
    const letterArr =
      'a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z,A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z'.split(
        ','
      )
    const code = ref('')
    const CharacterVerification = ref<HTMLDivElement | null>(null)
    const verifyCanvas = ref<HTMLCanvasElement | null>(null)
    // 生成验证码
    let ctx
    onMounted(() => {
      refresh()
    })
    const refresh = () => {
      code.value = ''
      if (verifyCanvas.value?.getContext) {
        ctx = verifyCanvas.value.getContext('2d') as CanvasRenderingContext2D
      } else {
        return
      }

      ctx.textBaseline = 'middle'

      ctx.fillStyle = randomColor(180, 240)
      ctx.fillRect(0, 0, props.width, props.height)

      if (props.type == 'blend') {
        // 判断验证码类型
        var txtArr = numArr.concat(letterArr)
      } else if (props.type == 'number') {
        var txtArr = numArr
      } else {
        var txtArr = letterArr
      }

      for (var i = 1; i <= 4; i++) {
        const txt = txtArr[randomNum(0, txtArr.length)]
        code.value += txt
        ctx.font = randomNum(props.height / 2, props.height) + 'px SimHei' // 随机生成字体大小
        ctx.fillStyle = randomColor(50, 160) // 随机生成字体颜色
        ctx.shadowOffsetX = randomNum(-3, 3)
        ctx.shadowOffsetY = randomNum(-3, 3)
        ctx.shadowBlur = randomNum(-3, 3)
        ctx.shadowColor = 'rgba(0, 0, 0, 0.3)'
        const x = (props.width / 5) * i
        const y = props.height / 2
        const deg = randomNum(-30, 30)
        /** 设置旋转角度和坐标原点**/
        ctx.translate(x, y)
        ctx.rotate((deg * Math.PI) / 180)
        ctx.fillText(txt, 0, 0)
        /** 恢复旋转角度和坐标原点**/
        ctx.rotate((-deg * Math.PI) / 180)
        ctx.translate(-x, -y)
      }
      /** 绘制干扰线**/
      for (var i = 0; i < 4; i++) {
        ctx.strokeStyle = randomColor(40, 180)
        ctx.beginPath()
        ctx.moveTo(randomNum(0, props.width), randomNum(0, props.height))
        ctx.lineTo(randomNum(0, props.width), randomNum(0, props.height))
        ctx.stroke()
      }
      /** 绘制干扰点**/
      for (var i = 0; i < props.width / 4; i++) {
        ctx.fillStyle = randomColor(0, 255)
        ctx.beginPath()
        ctx.arc(
          randomNum(0, props.width),
          randomNum(0, props.height),
          1,
          0,
          2 * Math.PI
        )
        ctx.fill()
      }
      //console.log(code.value)
    }

    /** 生成一个随机色**/
    const randomColor = (min: number, max: number) => {
      // eslint-disable-next-line @typescript-eslint/no-use-before-define
      const r = randomNum(min, max)
      // eslint-disable-next-line @typescript-eslint/no-use-before-define
      const g = randomNum(min, max)
      const b = randomNum(min, max)
      return 'rgb(' + r + ',' + g + ',' + b + ')'
    }
    /** 生成一个随机数**/
    const randomNum = (min: number, max: number) => {
      return Math.floor(Math.random() * (max - min) + min)
    }
    /** 验证验证码**/
    const validate = (iptCode: string) => {
      const newIptCode = iptCode.toLowerCase()
      const v_code = code.value.toLowerCase()
      // eslint-disable-next-line eqeqeq, @typescript-eslint/camelcase
      if (newIptCode == v_code) {
        console.log('验证码正确')
        return true
      } else {
        console.log('验证码错误')
        refresh()
        return false
      }
    }

    // 暴露方法
    expose({ validate })

    return {
      CharacterVerification,
      verifyCanvas,
      refresh,
      validate
    }
  }
})
</script>

<style lang="scss" scoped></style>
